package com.juli.school.service.impl;

import java.util.Date;
import java.util.List;
import java.util.UUID;

import javax.annotation.Resource;

import org.apache.commons.lang3.time.DateFormatUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.alibaba.druid.util.StringUtils;
import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import com.juli.school.dao.entity.PsychTest;
import com.juli.school.dao.mapper.AnswerDetailsMapper;
import com.juli.school.dao.mapper.PsychTestMapper;
import com.juli.school.dao.mapper.TestScopeMapper;
import com.juli.school.dao.mapper.TestSubjectMapper;
import com.juli.school.service.bo.PsychTestBo;
import com.juli.school.service.utils.BeanUtils;
import com.juli.school.utils.core.model.CommonResp;
import com.juli.school.utils.dao.base.Page;

@Service
public class PsychTestService {

	private static final Logger logger = LoggerFactory.getLogger(PsychTest.class);
	
	@Resource
	PsychTestMapper psyMapper;
	
	@Resource
	TestSubjectMapper testSubjectMapper;
	
	@Resource
	TestScopeMapper testScopeMapper;
	
	@Resource
	AnswerDetailsMapper answerDetailsMapper;
	
	public CommonResp<Boolean> add(PsychTestBo psychTestBo){
		CommonResp<Boolean> commonResp = new CommonResp<Boolean>();
		try {
			//发布时间
			Date date = new Date();
			String nowTime = DateFormatUtils.format(date, "yyyy-MM-dd HH:mm:ss");
			psychTestBo.setCreateTime(nowTime);
			psychTestBo.setSnStamp(UUID.randomUUID().toString());
			psyMapper.insert(psychTestBo);
			commonResp.success();
		} catch (Exception e) {
			logger.error("新增心理测试信息异常", e);
			commonResp.exception("新增心理测试信息异常");
		}
		return commonResp;
	}

	public CommonResp<Boolean> delete(String snStamp) {
		CommonResp<Boolean> commonResp = new CommonResp<Boolean>();
		try {
			if (StringUtils.isEmpty(snStamp)) {
				commonResp.fail("入参不能为空");
				return commonResp;
			}
			//删除心理测试数据
			psyMapper.deleteBySnStamp(snStamp);
			//删除题目数据
			testSubjectMapper.deleteBySnStamp(snStamp);
			//删除分数级别数
			testScopeMapper.deleteBySnStamp(snStamp);
			//删除学生答题情况数据
			answerDetailsMapper.deleteBySnStamp(snStamp);
			commonResp.success();
		} catch (Exception e) {
			logger.error("删除代跑腿信息异常", e);
			commonResp.exception("删除代跑腿信息异常");
		}
		return commonResp;
	}

	public CommonResp<List<PsychTestBo>> listPage(Integer pageNo) {
		CommonResp<List<PsychTestBo>> commonResp = new CommonResp<List<PsychTestBo>>();
		try {
			PsychTest psy = new PsychTest();
			Page page = new Page();
			page.setCurrentPage(Integer.valueOf(pageNo));
			page.setShowCount(20);
			psy.setPage(page);
			List<PsychTest> listPage = psyMapper.listPage(psy);
			List<PsychTestBo> data = Lists.newArrayList();
			for (PsychTest psychTest : listPage) {
				PsychTestBo psychTestBo = new PsychTestBo();
				BeanUtils.copyProperties(psychTestBo, psychTest);
				data.add(psychTestBo);
			}
			commonResp.success();
			commonResp.setData(data);
		} catch (Exception e) {
			logger.error("获取心理测试列表异常", e);
			commonResp.exception("获取心理测试列表异常");
		}
		return commonResp;
	}

	public CommonResp<PsychTest> detail(String snStamp) {
		CommonResp<PsychTest> commonResp = new CommonResp<PsychTest>();
		try {
			if (Strings.isNullOrEmpty(snStamp)) {
				commonResp.fail("入参不可为空");
				return commonResp;
			}
			PsychTest psychTest = new PsychTest();
			psychTest.setSnStamp(snStamp);
			PsychTest psychTestBo = psyMapper.get(psychTest);
			if (psychTestBo == null) {
				commonResp.exception("没有获取到信息");
				return commonResp;
			}
			commonResp.setData(psychTestBo);
			commonResp.success();
		} catch (Exception e) {
			logger.error("获取心理测试详情发生异常", e);
			commonResp.exception("获取心理测试详情发生异常");
		}
		return commonResp;
	}

	public CommonResp<Boolean> update(PsychTestBo psychTestBo) {
		CommonResp<Boolean> commonResp = new CommonResp<Boolean>();
		try {
			if(Strings.isNullOrEmpty(psychTestBo.getSnStamp())) {
				commonResp.fail("心理测试不可为空");
				return commonResp;
			}
			psyMapper.update(psychTestBo);
			commonResp.success();
		} catch (Exception e) {
			logger.error("更新心理测试信息异常", e);
			commonResp.exception("更新心理测试信息异常");
		}
		return commonResp;
	}
}
